import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

import CreatePersistedState from "vuex-persistedstate";

const store = new Vuex.Store({
	plugins: [
		CreatePersistedState({
			reducer(val) {
				return {
					// 只储存state中的assessmentData
					goodsCart: val.goodsCart,
					currentEqiupment: val.currentEqiupment
				}
			}
		})
	],
	state: {
		uerInfo: {},
		hasLogin: false,
		socketOpen: false,
		socketMsgQueue: [],
		fileManagement: [],
		adManagement: [],
		qeManagement: [],
		goodsManagement: [],
		jurisdiction: '',
		goodsCart: [],
		currentEqiupment: {
			eqiupment_id: undefined,
			eqiupment_name: "",
			address: ""
		}
	},
	getters: {
		goodsCart: state => {
			return state.goodsCart
		},
		currentEqiupment: state => {
			return state.currentEqiupment
		},
		hasLogin: state => {
			return state.hasLogin
		}
	},
	mutations: {
		FILE_MANAGEMENT: (state, routers) => {
			state.fileManagement = JSON.parse(JSON.stringify(routers))
		},
		QE_MANAGEMENT: (state, routers) => {
			state.qeManagement = JSON.parse(JSON.stringify(routers))
		},
		AD_MANAGEMENT: (state, routers) => {
			state.adManagement = JSON.parse(JSON.stringify(routers))
		},
		GOODS_MANAGEMENT: (state, routers) => {
			state.goodsManagement = JSON.parse(JSON.stringify(routers))
		},
		JURISDICTION: (state, routers) => {
			state.jurisdiction = JSON.parse(JSON.stringify(routers))
		},
		setSocketOpen(state, provider) {
			console.log('监测websokit连接', state, provider)
			state.socketOpen = provider
		},
		setSocketMsgQueue(state, provider) {
			console.log('监测websokit连接', state, provider)
			state.socketMsgQueue.push(provider)
		},
		login(state, provider) {
			state.hasLogin = true
			state.uerInfo = provider
			uni.setStorage({
				key: 'uerInfo',
				data: provider
			})
			if (typeof callback === 'function') {
				callback(res)
			}
		},
		//退出登录
		logout(state) {
			state.hasLogin = false
			state.uerInfo = {}
			uni.removeStorage({
				key: 'uerInfo'
			})
		},
		// 购物车
		addToCart(state, provider) {
			if (provider.type === 1) {
				state.goodsCart.push(provider.goodList)
			} else if (provider.type === 2) {
				state.goodsCart[provider.index].number++
			} else if (provider.type === 3) {
				state.goodsCart[provider.index].number--
			} else if (provider.type === 4) {
				state.goodsCart.splice(provider.index, 1)
			}else if(provider.type === 5){
				state.goodsCart = []
			}
		},
		// 当前设备
		currentEq(state, provider) {
			console.log('设备', provider);
			state.currentEqiupment.eqiupment_id = provider.id;
			state.currentEqiupment.eqiupment_name = provider.name;
			state.currentEqiupment.address = provider.address;
		}
	},
	actions: {

	}
})

export default store
